package com.sk.skplanet.commute.daofactory;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.sk.skplanet.commute.vo.Data;

public class LocalDaoFactory {
	private static final String FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + Data.FilePATH
			+ Data.FileNAME;

	public LocalDaoFactory() {
		if (Data.sqlite == null) Data.sqlite = SQLiteDatabase.openDatabase(FILE_PATH, null,
				SQLiteDatabase.OPEN_READWRITE);
	}

	public LocalDaoFactory(int num) {
		if (Data.sqlite == null) Data.sqlite = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory()
				.getAbsolutePath() + Data.FilePATH2 + Data.FileNAME, null, SQLiteDatabase.OPEN_READWRITE);
	}

	/** 테이블의 Create SQL문을 실행하여 테이블을 생성 */
	public static Cursor selectIDQuery(String data, String table_name) {
		String query = "";
		if (Data.TABLE_STATION.equals(table_name)) query = "select stationId, stationNameKor from station where stationNameKor like '%"
				+ data + "%'";
		else if (Data.TABLE_BUS.equals(table_name)) query = "select busId, busNumberKor, busType from bus where busNumberKor like '%"
				+ data + "%'";
		else if (Data.TABLE_CONFIG.equals(table_name)) query = "select key, value from config where key = '" + data
				+ "'";
		return (Cursor) Data.sqlite.rawQuery(query, null);
	}

	public static void updateAuthKey(String AuthKey) {
		String query = "update config set authKey = ";
		ContentValues arg = new ContentValues();
		arg.put("value", AuthKey);
		Data.sqlite.update("config", arg, "key = ?", new String[] { "authKey" });
	}
}